// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Sega Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value2 : object.trailPos.x
private alias object.value3 : object.levelSelectCheatPos
private alias object.value4 : object.anyCheatDone
private alias object.value5 : object.regionSwitchCheatPos

// States
private alias 0 : SEGA_SETUP
private alias 1 : SEGA_FADEIN
private alias 2 : SEGA_SONICRUN_LEFT
private alias 3 : SEGA_FADEOUTTRAIL_LEFT
private alias 4 : SEGA_SONICRUN_RIGHT
private alias 5 : SEGA_FADEOUTTRAIL_RIGHT
private alias 6 : SEGA_SHOWSEGALOGO
private alias 7 : SEGA_FADETOTITLE

// Game Modes
private alias 0 : MODE_NOSAVE

// Priority
public alias 0 : PRIORITY_BOUNDS
public alias 1 : PRIORITY_ACTIVE
public alias 2 : PRIORITY_ALWAYS
public alias 3 : PRIORITY_XBOUNDS
public alias 4 : PRIORITY_XBOUNDS_DESTROY
public alias 5 : PRIORITY_INACTIVE
public alias 6 : PRIORITY_BOUNDS_SMALL
public alias 7 : PRIORITY_ACTIVE_SMALL

// Ink Effects
public alias 0 : INK_NONE
public alias 1 : INK_BLEND
public alias 2 : INK_ALPHA
public alias 3 : INK_ADD
public alias 4 : INK_SUB

// Flip Directions
public alias 0 : FLIP_NONE
public alias 1 : FLIP_X
public alias 2 : FLIP_Y
public alias 3 : FLIP_XY

// Player List Pos Aliases
private alias 2 : PLAYER_KNUCKLES_A


// ========================
// Function Declarations
// ========================

reserve function Sega_CheckTouchCheatCodes


// ========================
// Function Definitions
// ========================

private function Sega_CheckTouchCheatCodes
	CheckTouchRect(0, 0, screen.xsize, screen.ysize)
	if checkResult == -1
		object.anyCheatDone = 0
	end if

	if object.anyCheatDone == false
		temp0 = screen.xcenter
		switch object.levelSelectCheatPos
		case 0
			temp0 -= 93
			temp1 = temp0
			temp1 += 39
			break
			
		case 1
			temp0 -= 50
			temp1 = temp0
			temp1 += 42
			break
			
		case 2
			temp0 -= 6
			temp1 = temp0
			temp1 += 42
			break
			
		case 3
			temp0 += 36
			temp1 = temp0
			temp1 += 58
			break
			
		end switch

		CheckTouchRect(temp0, 90, temp1, 150)
		if checkResult > -1
			object.anyCheatDone = true
			object.levelSelectCheatPos++
			if object.levelSelectCheatPos == 4
				if options.gameMode == MODE_NOSAVE
					PlaySfx(SfxName[Ring L], false)
					options.stageSelectFlag = true
				end if
			end if
		end if

		temp0 = screen.xcenter
		switch object.regionSwitchCheatPos
		case 0
			temp0 += 36
			temp1 = temp0
			temp1 += 58
			break
			
		case 1
			temp0 -= 6
			temp1 = temp0
			temp1 += 42
			break
			
		case 2
			temp0 -= 50
			temp1 = temp0
			temp1 += 42
			break
			
		case 3
			temp0 -= 93
			temp1 = temp0
			temp1 += 39
			break
			
		end switch
		
		CheckTouchRect(temp0, 90, temp1, 150)
		if checkResult > -1
			object.anyCheatDone = true
			object.regionSwitchCheatPos++
			if object.regionSwitchCheatPos == 4
				PlaySfx(SfxName[Ring L], false)
				options.region ^= 1
			end if
		end if
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
#platform: USE_ORIGINS
	if game.titleMode != false
		// If we're returning to the title, just skip the Sega screen and head straight for the actual Title
		game.titleMode = false
		ResetObjectEntity(object.entityPos, TypeName[ST Logo], 0, object.xpos, object.ypos)
	else
#endplatform
		switch object.state
		case SEGA_SETUP
			object.alpha 		= 0x00
			object.inkEffect 	= 2
			object.timer 		= 320
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
			object.state++
			break

		case SEGA_FADEIN
			if object.timer > 0
				object.timer -= 8
			else
				object.direction 	= FACING_LEFT
				object.alpha 		= 0x100
				object.trailPos.x 	= 256
				object.state++
			end if
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
			break

		case SEGA_SONICRUN_LEFT
			object.frame++
			object.frame &= 3

			if object.trailPos.x > -320
				object.trailPos.x -= 32
			else
				object.direction = FACING_RIGHT
				object.state++
			end if
			break

		case SEGA_FADEOUTTRAIL_LEFT
			if object.alpha > -0x40
				object.alpha -= 0x10
			else
				object.state++
			end if
			break

		case SEGA_SONICRUN_RIGHT
			object.frame++
			object.frame &= 3

			if object.trailPos.x < 320
				object.trailPos.x += 32
			else
				object.alpha = 0x100
				object.state++
			end if
			break

		case SEGA_FADEOUTTRAIL_RIGHT
			if object.alpha > -0x40
				object.alpha -= 0x10
			else
				object.state++
			end if
			break

		case SEGA_SHOWSEGALOGO
			object.timer++
			if object.timer == 160
				object.timer = 0
				object.state = SEGA_FADETOTITLE
			end if

			if object.timer == 2
				PlaySfx(SfxName[Sega Chant], false)
			end if

			CallFunction(Sega_CheckTouchCheatCodes)
			break

		case SEGA_FADETOTITLE
			if object.timer < 0x100
				object.timer += 8
				SetScreenFade(0x00, 0x00, 0x00, object.timer)
			else
				if stage.playerListPos == PLAYER_KNUCKLES_A
					ResetObjectEntity(object.entityPos, TypeName[K Logo], 0, object.xpos, object.ypos)
				else
					ResetObjectEntity(object.entityPos, TypeName[ST Screen], 0, object.xpos, object.ypos)
				end if
				SetScreenFade(0x00, 0x00, 0x00, 0xFF)
			end if
			break
			
		end switch
#platform: USE_ORIGINS
	end if // game.titleMode == false check from up above
#endplatform
end event


event ObjectDraw
	DrawRect(0, 0, screen.xsize, screen.ysize, 0xE0, 0xE0, 0xE0, 0xFF)

	switch object.state
	default
	case SEGA_SETUP
	case SEGA_FADEIN
		break

	case SEGA_SONICRUN_LEFT
		temp0 = object.trailPos.x
		temp0 += screen.xcenter
		DrawSpriteScreenFX(4, FX_FLIP, temp0, 121)
		DrawSpriteScreenFX(5, FX_FLIP, temp0, 121)
		DrawSpriteScreenFX(6, FX_FLIP, temp0, 121)
		DrawSpriteScreenFX(object.frame, FX_FLIP, temp0, screen.ycenter)
		break

	case SEGA_FADEOUTTRAIL_LEFT
		DrawSpriteScreenFX(7, FX_INK, 0, 121)
		DrawSpriteScreenFX(8, FX_INK, 0, 121)
		DrawSpriteScreenFX(9, FX_INK, 0, 121)

		temp0 = object.alpha
		object.alpha = 256
		object.alpha -= temp0
		DrawSpriteScreenFX(10, FX_INK, screen.xcenter, screen.ycenter)

		object.alpha = temp0
		break

	case SEGA_SONICRUN_RIGHT
		temp0 = object.trailPos.x
		temp0 += screen.xcenter
		DrawSpriteScreenFX(4, FX_FLIP, temp0, 120)
		DrawSpriteScreenFX(5, FX_FLIP, temp0, 120)
		DrawSpriteScreenFX(6, FX_FLIP, temp0, 120)
		DrawSpriteScreenXY(10, screen.xcenter, screen.ycenter)

		DrawSpriteScreenFX(object.frame, FX_FLIP, temp0, screen.ycenter)
		break

	case SEGA_FADEOUTTRAIL_RIGHT
		DrawSpriteScreenFX(7, FX_INK, 0, 120)
		DrawSpriteScreenFX(8, FX_INK, 0, 120)
		DrawSpriteScreenFX(9, FX_INK, 0, 120)

		DrawSpriteScreenXY(11, screen.xcenter, screen.ycenter)
		break

	case SEGA_SHOWSEGALOGO
	case SEGA_FADETOTITLE
		DrawSpriteScreenXY(11, screen.xcenter, screen.ycenter)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-60, -29, 100, 50, 1, 226)		// Rainbow Dashing - Frame 1 	- #0
	SpriteFrame(-60, -29, 100, 50, 102, 226)	// Rainbow Dashing - Frame 2 	- #1
	SpriteFrame(-60, -29, 100, 56, 203, 238)	// Rainbow Dashing - Frame 3 	- #2
	SpriteFrame(-60, -29, 100, 50, 162, 370)	// Rainbow Dashing - Frame 4 	- #3
	SpriteFrame(-192, -35, 256, 70, 1, 299)		// Rainbow Trail (Full) 	- #4
	SpriteFrame(-384, -35, 192, 70, 1, 299)		// Rainbow Trail - Frame 1 	- #5
	SpriteFrame(-576, -35, 192, 70, 1, 299)		// Rainbow Trail - Frame 2 	- #6
	SpriteFrame(0, -35, 192, 70, 1, 299)		// Rainbow Trail - Frame 3 	- #7
	SpriteFrame(192, -35, 192, 70, 1, 299)		// Rainbow Trail - Frame 4 	- #8
	SpriteFrame(384, -35, 192, 70, 1, 299)		// Rainbow Trail - Frame 5 	- #9
	SpriteFrame(-93, -29, 188, 58, 323, 297)	// Sega Logo (Interlaced)	- #10
	SpriteFrame(-93, -29, 188, 58, 323, 238)	// Sega Logo				- #11
	
	// Hide touch controls
	options.touchControls = false
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-93, -29, 188, 58, 323, 238)	// Sega Logo				- #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event

